//organization_heat_activity
exports.organization_heat_activity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    // + "/skip/" + skip + "/limit/" + limit
    var bulk_purchase_join_count = 0;
    //任务流创建
    async.waterfall([
            //机构活动
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationActivityOnly + "/oa_oid/" + restfulParams.o_id + "/sort_order/oa_order-asc" + "/skip/" + skip + "/limit/" + limit,
                        method: 'POST',
                        data: {
                            fields: ["oa_id", "oa_title", "oa_start_time", "oa_end_time", "oa_create_time", "oa_title_image", "oa_join_count", "oa_browse_count", "oa_title_image", "oa_title_image", "oa_type"].join(",")
                        }
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationActivityData = data.data;

                        cb();
                    }
                );

            },

            function (cb) {

                var tasks = [];

                //循环节点列表
                resultData.organizationActivityData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //拼团
                        if (item.oa_type == 0) {

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_bulk_purchase_activity_config + "/bpac_oaid/" + item.oa_id,
                                    method: 'POST',
                                    data: {
                                        fields: ["bpac_id", "bpac_type", "bpac_participation_price", "bpac_open_price"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //console.log(data);

                                    if (data.data.length > 0) {

                                        if (data.data[0].bpac_type == 0 || data.data[0].bpac_type == 1) {
                                            item.price = data.data[0].bpac_open_price;
                                        } else {
                                            item.price = data.data[0].bpac_participation_price;
                                        }

                                        if (item.price != '') {

                                            var priceArr = item.price.split("|");

                                            item.price = priceArr[priceArr.length - 1];

                                        } else {

                                            item.price = 0;

                                        }


                                        R.SEND_HTTP(
                                            req,
                                            {
                                                url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_oaid/" + item.oa_id + "/count/1",
                                                method: 'POST',
                                                data: {
                                                    fields: ["u_nickname"].join(",")
                                                }
                                            },
                                            function (err, data) {
                                                //判断是否有错误信息
                                                if (err) {
                                                    return cb(err, data);
                                                }

                                                item.bulk_purchase_join_count = data.count;
                                                cb();
                                            }
                                        );

                                    } else {
                                        cb();
                                    }


                                }
                            );
                            //投票
                        } else if (item.oa_type == 1) {
                            item.price = 0;
                            cb();
                            //砍价价格
                        } else if (item.oa_type == 2) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_bargain_config + "/bc_oaid/" + item.oa_id,
                                    method: 'POST',
                                    data: {
                                        fields: ["bc_most_price"].join(",")
                                    }
                                },
                                function (err, data) {

                                    // console.log(111,data);
                                    if (data.data.length > 0) {
                                        item.price = data.data[0].bc_most_price;
                                    }
                                    cb();

                                }
                            );
                        } else if (item.oa_type == 3) {
                            item.price = 0;
                            cb();
                        } else if (item.oa_type == 4) {
                            item.price = 0;
                            cb();
                        } else if (item.oa_type == 5) {

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_discounts_activity_config + "/dac_oaid/" + item.oa_id,
                                    method: 'POST',
                                    data: {
                                        fields: ["dac_id", "dac_new_price"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //console.log(data);

                                    if (data.data.length > 0) {
                                        item.price = data.data[0].dac_new_price;
                                    }


                                    cb();
                                }
                            );
                        } else if (item.oa_type == 6) {
                            item.price = 0;
                            cb()
                        } else {
                            cb();
                        }


                    });

                });

                async.waterfall(tasks, cb);

            }


        ], function (err, result) {

            RES.response(res, true, resultData);

        }
    );

};